package Controller.Hadoop;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import Bean.MemberBean;
import Bean.areaGGLBean;
import Bean.areaGSLBean;
import Bean.arealikeBean;
import Bean.monthGLBean;
import Bean.musicALBean;
import Bean.musicGLBean;
import Bean.singerALBean;
import Dao.adminDao;

public class hadoopEcho implements Controller {
	private adminDao dao;
	private singerALBean bean = new singerALBean();
	
	public void setDao(adminDao dao) {
		this.dao = dao;
	}

	@SuppressWarnings("null")
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse arg1) throws Exception {
		HttpSession session = request.getSession();
		String m_id = (String) session.getAttribute("member");
		String s_no = request.getParameter("s_no");
		String ms_no = request.getParameter("ms_no");
		String ms_genre = request.getParameter("ms_genre");
		String[] genre = ms_genre.split("/");
		System.out.println(genre[0]);
		String genreNo = dao.genreNo(genre[0]);
		
		// ------------최신 1달간 발매된 노래 찾기------------------
		Calendar cal = java.util.Calendar.getInstance();
		// 현재 년도, 월, 일
		String year = Integer.toString(cal.get(cal.YEAR));
		String month = Integer.toString(cal.get(cal.MONTH) + 1);
		month = month.length() > 1 ? month : "0" + month;
		String day = Integer.toString(cal.get(cal.DAY_OF_MONTH));
		day = day.length() > 1 ? day : "0" + day;

		String nowYMD = year +","+ month +","+ day; // 현재 yyyymmdd
		
		//--- 나이 구하기---------------------------------------------------
		MemberBean bean = dao.ssnSearch(m_id);
		String ssn1 = "19"+bean.getM_ssn1().toString();
		System.out.println("탄생년도 : "+ssn1);
		int year1 = Integer.parseInt(ssn1.substring(0, 4));
		int year2=  Integer.parseInt(year);
		int age =year2-year1;
		System.out.println("나이 :"+age+" : "+year1);
		//--- 성별 구하기 --------------------------------------------------
		String ssn2 = bean.getM_ssn2().toString();
		System.out.println("성별 : "+ssn2);
		int ssnGender = Integer.parseInt(ssn2.substring(0,1));
		if(ssnGender ==1 || ssnGender ==3){
			ssnGender = 1;
		}else{
			ssnGender = 2;
		}
		String area = dao.koreaarea(bean.getM_addres());
		
		String context = "";
		context = ssnGender+","+age+","+area+","+nowYMD+","+s_no+","+ms_no+","+genreNo;
		
		String[] cmd = { "echo '" + context
				+ "' >> /home/hadoop/Lab/bigmusic.csv" };

		System.out.println("하둡 연결");
		hadoopConect hadoop = new hadoopConect();
		System.out.println("리눅스 쓰기");
		// System.out.println(command.get(selectCom)+"하둡 명령어");
		System.out.println(cmd[0]);
		List<String> ret = hadoop.execs(cmd);

		System.out.println("쓰기 끝 ");

		//카운터 증가
		dao.countUp(ms_no);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/audio.music");
		return mav;
	}
}
